﻿using PineLake.Drmwe.SystemConfig;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace PineLake.Drmwe.Encrypts.DES
{
    /// <summary>
    /// DES加密/解密类.
    /// </summary>
    public class DES : IDES
    {
        private static readonly String DefaultKey = !string.IsNullOrWhiteSpace(AppSettings.DESKey) ? AppSettings.DESKey : "Pine&Lake$MM2308*PlSoft&DES$";  //默认密钥需要长度设置为8位以上

        #region 加密

        /// <summary>
        /// 加密数据,启用默认密匙.
        /// </summary>
        /// <param name="EncryptStr">加密字符</param>
        /// <returns></returns>
        public string Encrypt(string EncryptStr)
        {
            return Encrypt(EncryptStr, DefaultKey);
        }
        /// <summary> 
        /// 加密数据,启用自定义密匙.
        /// </summary> 
        /// <param name="EncryptStr">加密数据</param> 
        /// <param name="sKey">自定义密匙</param> 
        /// <returns></returns>
        /// 
        public string Encrypt(string EncryptStr, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            try
            {
                byte[] inputByteArray;
                inputByteArray = Encoding.Default.GetBytes(EncryptStr);
                des.Key = Encoding.UTF8.GetBytes(SHA1Hash.SHA1.Encrypt(sKey)[..8]);
                des.IV = Encoding.UTF8.GetBytes(SHA1Hash.SHA1.Encrypt(sKey)[..8]);
                des.Mode = CipherMode.CBC;
                des.Padding = PaddingMode.PKCS7;
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                return ret.ToString();
            }
            catch (Exception)
            {
                return "403";
            }
            finally
            {
                des.Dispose();
            }
        }

        #endregion

        #region 解密


        /// <summary>
        /// 解密数据,启用默认密匙.
        /// </summary>
        /// <param name="DecryptStr">解密数据</param>
        /// <returns></returns>
        /// 
        public string Decrypt(string DecryptStr)
        {
            return Decrypt(DecryptStr, DefaultKey);
        }
        /// <summary> 
        /// 解密数据,启用自定义密匙. 
        /// </summary> 
        /// <param name="DecryptStr">解密数据</param> 
        /// <param name="sKey">自定义密匙</param> 
        /// <returns></returns> 
        /// 
        public string Decrypt(string DecryptStr, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            try
            {
                int len;
                len = DecryptStr.Length / 2;
                byte[] inputByteArray = new byte[len];
                int x, i;
                for (x = 0; x < len; x++)
                {
                    i = Convert.ToInt32(DecryptStr.Substring(x * 2, 2), 16);
                    inputByteArray[x] = (byte)i;
                }
                des.Key = Encoding.UTF8.GetBytes(SHA1Hash.SHA1.Encrypt(sKey).Substring(0, 8));
                des.IV = Encoding.UTF8.GetBytes(SHA1Hash.SHA1.Encrypt(sKey).Substring(0, 8));
                des.Mode = CipherMode.CBC;
                des.Padding = PaddingMode.PKCS7;
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Encoding.Default.GetString(ms.ToArray());
            }
            catch (Exception)
            {
                return "403";
            }
            finally
            {
                des.Dispose();
            }
        }

        #endregion
    }
}
